package com.cenwangkeji.resident.ui.activity;

import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.ServicePackageEvaluateDetailBean;
import com.cenwangkeji.resident.model.bean.SuccessBean;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.ui.adapter.ServicePackageEvaluateDetailAdapter;
import com.google.gson.Gson;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//服务包评价详情
public class ServicePackageEvaluateDetailActivity extends BaseActivity {

    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.rv)
    RecyclerView mRv;
    @BindView(R.id.state)
    TextView mState;
    @BindView(R.id.tv_content)
    TextView mTvContent;
    @BindView(R.id.cl_list)
    ConstraintLayout mClList;
    @BindView(R.id.tv_name1)
    TextView mTvName1;
    @BindView(R.id.cl_evaluate)
    ConstraintLayout mClEvaluate;
    @BindView(R.id.et_content)
    EditText mEtContent;//评价内容
    private int level = 1;//默认选中满意评价

    @Override
    public int setLayoutId() {
        return R.layout.activity_service_package_evaluate_detail;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        getDate();

    }

    private void getDate() {
        showLoading();
        Observable.create(new ObservableOnSubscribe<ServicePackageEvaluateDetailBean>() {
            @Override
            public void subscribe(ObservableEmitter<ServicePackageEvaluateDetailBean> emitter) throws Exception {
                int id = getIntent().getIntExtra("id", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put(Constant.token, User.getToken());
                map.put("id", id);
                map.put("mark", 3);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.service_package_evaluate_detail);
                Gson gson = new Gson();
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, ServicePackageEvaluateDetailBean.class));
            }
        }).compose(RxUtils.<ServicePackageEvaluateDetailBean>io_main()).subscribe(new ObserverOnNext<ServicePackageEvaluateDetailBean>() {
            @Override
            public void onNext(ServicePackageEvaluateDetailBean servicePackageEvaluateDetailBean) {
                if (servicePackageEvaluateDetailBean.getCode() == 0) {
                    if (servicePackageEvaluateDetailBean.getData()!= null&&servicePackageEvaluateDetailBean.getData().getCsi()==0){
                        mClEvaluate.setVisibility(View.VISIBLE);
                        mClList.setVisibility(View.INVISIBLE);
                        mTvName1.setText("服务名称："+servicePackageEvaluateDetailBean.getData().getName());

                    }else {
                        mClEvaluate.setVisibility(View.INVISIBLE);
                        mClList.setVisibility(View.VISIBLE);
                        mTvName.setText("服务名称："+servicePackageEvaluateDetailBean.getData().getName());
                        mRv.setLayoutManager(new LinearLayoutManager(ServicePackageEvaluateDetailActivity.this));
                        mRv.setAdapter(new ServicePackageEvaluateDetailAdapter(servicePackageEvaluateDetailBean.getData().getDzbUserRecordDOS()));
                        switch (servicePackageEvaluateDetailBean.getData().getCsi()){
                            case 1:
                                mState.setText("我的评价：满意");//我的评价
                                break;
                            case 2:
                                mState.setText("我的评价：一般");
                                break;
                            case 3:
                                mState.setText("我的评价：不满意");
                                break;
                        }
                        mTvContent.setText(servicePackageEvaluateDetailBean.getData().getEvaluate());

                    }

                } else {
                    MyToast.show(servicePackageEvaluateDetailBean.getMsg());
                }
                dismissLoading();
            }
        });
    }

    public void onEvaluate(View view) {
        switch (view.getId()){
            case R.id.rb_1:
                level =1;
                break;
            case R.id.rb_2:
                level =3;
                break;
            case R.id.rb_3:
                level =3;
                break;
        }
    }


    @OnClick(R.id.btn)
    public void onViewClicked() {
        final String content = mEtContent.getText().toString();
        if (TextUtils.isEmpty(content)){
            MyToast.show("内容不能为空");
        }else {
            Observable.create(new ObservableOnSubscribe<SuccessBean>() {
                @Override
                public void subscribe(ObservableEmitter<SuccessBean> emitter) throws Exception {
                    int id = getIntent().getIntExtra("dzbId", 0);
                    Map<Object, Object> map = new HashMap<>();
                    map.put(Constant.token, User.getToken());
                    map.put("evaluate", content);
                    map.put("mark", 3);
                    map.put("csi", level);
                    map.put("grId", User.getUserId());
                    map.put("dzbId", id);
                    JSONObject jsonObject = new JSONObject(map);
                    MyLogger.i(jsonObject.toString());
                    String json = OkHttpUtils.post(jsonObject.toString(), Constant.add_service_package_evaluate);
                    MyLogger.i(json);
                    Gson gson = new Gson();
                    emitter.onNext(gson.fromJson(json, SuccessBean.class));
                }
            }).compose(RxUtils.<SuccessBean>io_main()).subscribe(new ObserverOnNext<SuccessBean>() {
                @Override
                public void onNext(SuccessBean bean) {
                if (bean.getCode()==0){
                   MyToast.show("评价成功");
                   finish();
                }else {
                   MyToast.show(bean.getMsg());
                }

                }
            });
        }
    }
}
